package thread.threaddemo;

/**
 * @Author: wenjingyuan
 * @Date: 2023/01/14/23:35
 * @Description:notifyAll（）方法
 */
public class WaitDemo3 {
    public static void main(String[] args) {
        Object lock=new Object();
        Object lock2=new Object();
        Thread t1=new Thread(()->{
            //
            System.out.println("线程1开始执行");
            try {
                synchronized (lock){
                    System.out.println("线程1调用wait方法");
                    lock.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程1结束执行");
        },"线程1");
        t1.start();

        Thread t2=new Thread(()->{
            //
            System.out.println("线程2开始执行");
            try {
                synchronized (lock){
                    System.out.println("线程2调用wait方法");
                    lock.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2结束执行");
        },"线程2");
        t2.start();

        Thread t3=new Thread(()->{
            //
            System.out.println("线程3开始执行");
            try {
                synchronized (lock){
                    System.out.println("线程3调用wait方法");
                    lock.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程3结束执行");
        },"线程3");
        t3.start();

        //线程4执行唤醒线程
        Thread t4=new Thread(()->{
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程4开始执行");
            synchronized (lock){
                lock.notifyAll();
                System.out.println("线程4执行唤醒操作");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程4 执行结束");
            }
        },"线程4");
        t4.start();
    }
}
